<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Node extends Base {

    public function initialize(){
        parent::initialize();
		$this->model = new \app\admin\model\NodeM;
    }

    public function index(){
        return $this->fetch();
    }
	public function lists(){
		$data = request()->param();
		$res = Db::name('node')->where('module_name','admin')->select();
		$arr =[];
			foreach($res as $key=>$row){
				$arr[$key] = $row;
				if(isset($data['id'])){
					if($row['id'] == $data['id'] || $row['parentId'] == $data['id']){
						$arr[$key]['disabled'] = true; 
					}
				}
		
			}
			$this->reJson['code'] = 200;
			$this->reJson['data'] = $arr;
		return json($this->reJson);
	}
	public function add(){
		if(request()->isPost()){
			$res = $this->model->add();
			if($res){
				$this->reJson['code'] = 200;
				$this->reJson['msg']= '操作成功!';
			}
			return json($this->reJson);
		}else{
			return $this->fetch();
		}
	}
	public function edit(){
		if(request()->isPost()){
			$res = $this->model->edit();
			if($res){
				$this->reJson['code'] = 200;
				$this->reJson['msg']= '操作成功!';
			}
			return json($this->reJson);
		}else{
			$id = request()->param('id');
			if($id){
				$this->assign('d_json',json_encode(Db::name('node')->where('id',$id)->find()));
				return $this->fetch();				
			}else{
				return $this->fetch('node/add');
			}
		}		
	}
	public function del(){
			$res = $this->model->del();
			if($res){
				$this->reJson['code'] = 200;
				$this->reJson['msg']= '操作成功!';
			}
			return json($this->reJson);
	}
}
